﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

using Continental.Code.BL;
using Continental.Code.Entity;
using Continental.Code.Utility;

namespace Continental.UI.TuDiem.Control
{
    public partial class TopMenu : System.Web.UI.UserControl
    {
        public Int32 nhomID = -1;
        public Int32 parentID = -1;
        public Int32 countCurrent = -1;
        private StringBuilder sbMain = new StringBuilder();
        private StringBuilder sbTabContent = new StringBuilder();

        private DataTable tblNhomTuDiem;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                tblNhomTuDiem = TuDiemProvider.GetNhomTuDiemList().Tables[0];

                NhomTuDiem theNhomTuDiem = TuDiemProvider.GetNhomTuDiemByID(nhomID);

                if (theNhomTuDiem != null)
                    parentID = theNhomTuDiem.ParentID;
                else
                {
                    parentID = -1;
                    nhomID = -1;
                }

                DataTable tblNhomTuDiemLevel0 = tblNhomTuDiem.Select("ParentID = 0").CopyToDataTable();
                Int32 count = 0;

                foreach (DataRow dr in tblNhomTuDiemLevel0.Rows)
                {
                    Int32 id = Int32.Parse(dr["NhomID"].ToString());
                    String idRel = "ct" + count.ToString();
                    String css;
                    Boolean isCurrent;

                    if (id == nhomID || id == parentID)
                    {
                        css = "current";
                        isCurrent = true;
                        countCurrent = count;
                    }
                    else
                    {
                        css = "";
                        isCurrent = false;
                    }

                    sbMain.Append("<li>");
                    sbMain.Append("<a class='" + css + "' rel='" + idRel + "' href='/Tu-Diem/" + HelpMethod.ConvertToUnSignForUrl(dr["TitleOrName"].ToString()) + "/" + id.ToString() + ".aspx'>");
                    sbMain.Append("<div class='headspacer2'>");
                    sbMain.Append(dr["TitleOrName"].ToString());
                    sbMain.Append("</div>");
                    sbMain.Append("</a>");
                    sbMain.Append("</li>");
                    GenerateTabMenuContentByParentID(id, count, isCurrent);
                    count += 1;
                }

                this.ltrMain.Text = sbMain.ToString();
                this.ltrTabContent.Text = sbTabContent.ToString();
            }
        }

        private void GenerateTabMenuContentByParentID(Int32 parentID, Int32 count, Boolean isCurrent)
        {
            String style = "";

            if (isCurrent)
                style = "margin-top: 0px; margin-right: 0px; margin-bottom: 0px;  margin-left: 0px; padding-top: 9px; display: block";
            else
                style = "margin-top: 0px; margin-right: 0px; margin-bottom: 0px;  margin-left: 0px; padding-top: 9px; display: none";

            sbTabContent.Append("<div id='ct" + count.ToString() + "' class='tabcontent' style='" + style + "'>");
            DataTable tblNhomTuDiemLevel1 = new DataTable();
            try
            {
                tblNhomTuDiemLevel1 = tblNhomTuDiem.Select("ParentID = " + parentID.ToString()).CopyToDataTable();
            }
            catch
            {
            }

            foreach (DataRow dr in tblNhomTuDiemLevel1.Rows)
            {
                Int32 id = Int32.Parse(dr["NhomID"].ToString());
                String titleOrName = dr["TitleOrName"].ToString();
                sbTabContent.Append("<div style='height: 22px; float: left;'>");
                sbTabContent.Append("<img src='/CSS/Images/bullet.jpg' width='3' height='3' alt='" + titleOrName + "' style='padding: 0' />");
                sbTabContent.Append("<a href='/Tu-Diem/" + HelpMethod.ConvertToUnSignForUrl(dr["TitleOrName"].ToString()) + "/" + id.ToString() + ".aspx'>");
                sbTabContent.Append("<font color='#b23743'>");
                sbTabContent.Append("<b>" + titleOrName.Substring(0, 1) + "</b>");
                sbTabContent.Append("</font>");
                sbTabContent.Append(titleOrName.Substring(1));
                sbTabContent.Append("</a>");
                sbTabContent.Append("</div>");
            }

            sbTabContent.Append("</div>");
        }
    }
}